/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.ASColor;
import org.aswing.BorderLayout;
import org.aswing.JMenuItem;
import org.aswing.JPanel;
import org.aswing.JSeparator;
import org.aswing.util.Delegate;

import com.bourre.events.BubbleEvent;
import com.bourre.events.EventType;

import fever.app.accelerator.Keyboard;
import fever.app.accelerator.KeyCombo;
import fever.app.local.LocalisationEvent;
import fever.data.file.FileFilter;

import fvaswing.components.menu.FvMenu;
import fvaswing.components.menu.FvMenuItem;
import fvaswing.components.reader.abstract.FvReader;
import fvaswing.components.reader.abstract.FvReaderEvent;
import fvaswing.components.reader.paper.FvFlashPaper;
import fvaswing.components.reader.paper.FvPaperItem;
import fvaswing.components.reader.paper.FvPaperList;
import fvaswing.components.reader.paper.FvPaperUIElement;
import fvaswing.FvAsWing;

/**
 * Display and manage Adobe FlashPaper document.
 * 
 * <p>Example ( append item in source code )
 * {@code
 *   var paper : FvPaperReader = new FvPaperReader( "myReader", true );
 *   paper.addDocument( new FvPaperItem( "assets/paperdoc.swf", "monTitre" ) );
 *   paper.addDocument( new FvPaperItem( "assets/code.swf") );
 *   paper.addDocument( new FvPaperItem( "assets/WineCellar.swf", "Test erreur" ) );
 *   
 *   paper.open( );   
 * }
 * 
 * <p>Example ( load document playlist )
 * {@code
 *   var paper : FvPaperReader = new FvPaperReader( "myReader", true );
 *   
 *   paper.load( "playlist.xml" );   
 * }
 * @author Romain Ecarnot
 */
class fvaswing.components.reader.FvPaperReader extends FvReader
{
	//-------------------------------------------------------------------------
	// Events definition
	//-------------------------------------------------------------------------
	
	/** Broadcasted when FlashPaper UI elements configuration change. */
	public static var onUIChangedEVENT : EventType = new EventType( "onUIChanged" );
	
		
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : FvPaperReader;
	
	private var _uiMap : Object;
	
	private var _printItem : FvMenuItem;
	private var _popItem : JMenuItem;
	private var _previousItem : FvMenuItem;
	private var _nextItem : FvMenuItem;
	private var _fitPageItem : JMenuItem;
	private var _fitToWidthItem : JMenuItem;
	private var _zoomPlusItem : FvMenuItem;
	private var _zoomMinusItem : FvMenuItem;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns unique {@link FvPaperReader} instance.
	 */
	public static function getInstance( enabledExternalLoading : Boolean ) : FvPaperReader
	{
		if( _instance ) _instance = new FvPaperReader( "paper", enabledExternalLoading );
		return _instance;
	}
	
	/**
	 * Constructor.
	 */
	public function FvPaperReader( name : String, enabledExternalLoading : Boolean )
	{
		super( name, enabledExternalLoading );
		
		_initUIElementMap();
	}
	
	/**
	 * Returns current {@link fvaswing.components.reader.paper.FvFlashPaper} 
	 * instance.
	 */
	public function getDocumentManager() : FvFlashPaper
	{
		return FvFlashPaper( _document );
	}
	
	/**
	 * Adds passed-in {@link fvaswing.components.reader.paper.FvPaperItem} 
	 * to reader list.
	 */
	public function addDocument( doc : FvPaperItem ) : Void
	{
		_list.addDocument( doc );
	}
	
	/**
	 * Removes passed-in {@link fvaswing.components.reader.paper.FvPaperItem} 
	 * from reader list.
	 */
	public function removeDocument( doc : FvPaperItem ) : Void
	{
		_list.removeDocument( doc );
	}
	
	/**
	 * Defines visible / hidden part of FlashPaper UI.
	 * 
	 * @param part {@link fvaswing.components.reader.paper.FvPaperUIElement} UI Element
	 * @param flag visibility flag
	 */
	public function showUIElement( part : FvPaperUIElement, flag : Boolean ) : Void
	{
		if( _uiMap.containsKey( part ) )
		{
			_uiMap.put( part, flag );
			_fireUIEvent();
		}
	}
	
	/**
	 * Triggered when Localisation language change.
	 */
	public function onLocalisationUpdate( event : LocalisationEvent ) : Void
	{
		super.onLocalisationUpdate( event );
		
		_printItem.setText( _resource.paperPrint );
		_previousItem.setText( _resource.paperPrevious );
		_nextItem.setText( _resource.paperNext );
		_fitPageItem.setText( _resource.paperFitPage );
		_fitToWidthItem.setText( _resource.paperFitWidth );
		_zoomPlusItem.setText( _resource.paperZoomPlus );
		_zoomMinusItem.setText( _resource.paperZoomMinus );
		_popItem.setText( _resource.paperOpenBrowser );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Overrides for custom playlist
	 */
	private function _getPlayList() : FvPaperList
	{
		return new FvPaperList( _name, _enabledExternalFile );
	}
	
	private function _configureChooser() : Void
	{
		super._configureChooser();
		
		var filter : FileFilter = new FileFilter( 'FlashPaper document' );
		filter.addExtension( 'swf' );
		
		_fileChooser.addChoosableFileFilter( filter );
		_fileChooser.setAcceptAllFileFilterUsed( false );
	}
	
	private function _initUIElementMap() : Void
	{
		var b : Boolean = true;
		
		_uiMap = new Object();
		_uiMap[ FvPaperUIElement.PrevNextElement ] = b;		_uiMap[ FvPaperUIElement.toolElement ] = b;		_uiMap[ FvPaperUIElement.zoomElement ] = b;		_uiMap[ FvPaperUIElement.findElement ] = b;		_uiMap[ FvPaperUIElement.sidebarElement ] = b;		_uiMap[ FvPaperUIElement.pageElement ] = b;		_uiMap[ FvPaperUIElement.overflowElement ] = b;	}
	
	private function _createDocumentPanel() : JPanel
	{
		_documentContainer = new JPanel( new BorderLayout( 5, 5 ) );
		_documentContainer.setOpaque( true );
		_documentContainer.setBackground( ASColor.WHITE );
		_documentContainer.addEventListener( JPanel.ON_CREATED, _onPanelCreated, this );		_documentContainer.addEventListener( JPanel.ON_RESIZED, _handleResizing, this );
		
		return _documentContainer;
	}
	
	private function _onPanelCreated( source : JPanel ) : Void
	{
		_loaderView = _documentContainer.createMovieClip( "__pt__" );
		_target = _documentContainer.createMovieClip( "__pt__" );
		
		_initDocumentManager( new FvFlashPaper( this, _target ) );
		
		if( _pendingLoad )
		{
			_pendingLoad = false;
			_document.load( _pendingItem );
		}
	}
	
	private function _update( event : FvReaderEvent ) : Void
	{
		var item : FvPaperItem = getDocumentManager().getDocument();
		
		item.setTitle( item.getTitle() );
		item.setNumberOfPages( getDocumentManager().getNumberOfPages() );
		_list.updateDocument( item );
		
		_frame.setTitle( item.getTitle() );
	}
	
	private function _fireUIEvent() : Void
	{
		var event : BubbleEvent = new BubbleEvent( onUIChangedEVENT, _uiMap );
		event.bubbles = true;
		broadcastEvent( event );
	}
	
	
	//-------------------------------------------------------------------------
	// Menubar
	//-------------------------------------------------------------------------
	
	/**
	 * Overrides for custom document type.
	 */
	private function _getDocumentMenu() : FvMenu
	{
		_docMenu = new FvMenu( _resource.docMenuLabel );
		_docMenu.setEnabled( false );
		
		_printItem = _docMenu.addMenuItem( _resource.paperPrint, _handlePrint, this  );
		
		_docMenu.append( new JSeparator( JSeparator.HORIZONTAL ) );
		
		_previousItem = _docMenu.addMenuItem( _resource.paperPrevious, _handlePreviousPage, this, new KeyCombo( Keyboard.onKeyCONTROL, Keyboard.onKeyLEFT ) );
		_keyMap.registerCustomType( FvAsWing.AWSHORTCUT, _previousItem.getShortCut(), _previousItem );
		
		_nextItem = _docMenu.addMenuItem( _resource.paperNext, _handleNextPage, this, new KeyCombo( Keyboard.onKeyCONTROL, Keyboard.onKeyRIGHT ) );
		_keyMap.registerCustomType( FvAsWing.AWSHORTCUT, _nextItem.getShortCut(), _nextItem );
		
		_docMenu.append( new JSeparator( JSeparator.HORIZONTAL ) );
		
		_fitPageItem = _docMenu.addMenuItem( _resource.paperFitPage, _handleZoomPage, this  );
		_fitPageItem.addActionListener( Delegate.create( this, _handleZoomPage, "page" ) );
		
		_fitToWidthItem = _docMenu.addMenuItem( _resource.paperFitWidth );
		_fitToWidthItem.addActionListener( Delegate.create( this, _handleZoomPage, "width" ) );
		
		_docMenu.append( new JSeparator( JSeparator.HORIZONTAL ) );
		
		_zoomPlusItem = _docMenu.addMenuItem( _resource.paperZoomPlus, _handleZoomIn, this, Keyboard.onKeyADD );
		_keyMap.registerCustomType( FvAsWing.AWSHORTCUT, _zoomPlusItem.getShortCut(), _zoomPlusItem );
		
		_zoomMinusItem = _docMenu.addMenuItem( _resource.paperZoomMinus, _handleZoomOut, this, Keyboard.onKeySUBSTRACT );
		_keyMap.registerCustomType( FvAsWing.AWSHORTCUT, _zoomMinusItem.getShortCut(), _zoomMinusItem );
		
		_docMenu.append( new JSeparator( JSeparator.HORIZONTAL ) );
		
		_popItem = _docMenu.addMenuItem( _resource.paperOpenBrowser );
		_popItem.addActionListener( _handlePopUp, this );
		
		return _docMenu;
	}
	
	private function _handlePrint( source : JMenuItem ) : Void
	{
		getDocumentManager().printTheDocument();	
	}
	
	private function _handleNextPage( source : JMenuItem ) : Void
	{
		getDocumentManager().setCurrentPage( getDocumentManager().getCurrentPage() + 1 );	
	}
	
	private function _handlePreviousPage( source : JMenuItem ) : Void
	{
		getDocumentManager().setCurrentPage( getDocumentManager().getCurrentPage() - 1 );	
	}
	
	private function _handleZoomPage( source : JMenuItem, contant : String ) : Void
	{
		getDocumentManager().setCurrentZoom( contant );
	}
	
	private function _handleZoomIn( source : JMenuItem ) : Void
	{
		getDocumentManager().zoomIn();
	}
	
	private function _handleZoomOut( source : JMenuItem ) : Void
	{
		getDocumentManager().zoomOut();
	}
	
	private function _handlePopUp( source : JMenuItem ) : Void
	{
		getDocumentManager().popUpDocument();	
	}
	
}